<template>
	<view class="sesult">
		<u-sticky>
			<js-search-bar v-model="keyword" @clear="keyword = ''" @search="search"></js-search-bar>
			<view class="filtrate">
				<view class="item" :class="{ active: nowVal.name === item.name }" @click="handleItem(item)" v-for="(item, i) in tabs" :key="i">
					<view class="sximg" v-if="item.sx">
						<image src="@/static/images/sx.png" mode="aspectFit"></image>
					</view>
					<text>{{ item.name }}</text>
					<view class="glimg" v-if="item.filter">
						<image v-if="nowVal.id === item.id && item.val === 1" src="@/static/images/ssja.png" mode="aspectFit"></image>
						<image v-else src="@/static/images/ssj.png" mode="aspectFit"></image>

						<image v-if="nowVal.id === item.id && item.val === 2" src="@/static/images/xsja.png" mode="aspectFit"></image>
						<image v-else src="@/static/images/xsj.png" mode="aspectFit"></image>
					</view>
				</view>
			</view>
		</u-sticky>

		<view class="container">
			<goodList :list="list" :listStyle="3"></goodList>
			<u-loadmore :status="status" />
		</view>
	</view>
</template>

<script>
import API from '@/common/api';
export default {
	name: 'sesult',
	data() {
		return {
			keyword: '',
			status: '',
			tabs: [
				{
					id: 1,
					name: '综合',
					order1: 'ordering'
				},
				{
					id: 2,
					name: '销量',
					filter: true,
					val: 1,
					order1: 'orders'
				},
				{
					id: 3,
					name: '价格',
					filter: true,
					val: 1,
					order1: 'price_sale'
				}
				// {
				// 	id: 4,
				// 	name: '筛选',
				// 	sx: true,
				// 	order1: 'ordering'
				// }
			],
			nowVal: {
				id: 1,
				name: '综合'
			},
			list: [],
			paging: {
				page: 1,
				pagenum: 20
			},
			tags: ''
		};
	},
	onLoad(data) {
		this.reset();
		if (data.keyword) this.keyword = decodeURIComponent(data.keyword) || '';
		if (data.hasOwnProperty('tags')) {
			this.tags = data.tags;
			uni.setNavigationBarTitle({
				title: data.tags
			});
		}
		this.getList();
	},
	onReachBottom() {
		if (this.status === 'nomore') return;
		this.getList();
	},
	methods: {
		search(e) {
			this.reset();
			this.keyword = e;
			this.getList();
		},

		getList() {
			API.getJson('product_plist', {
				tags: this.tags,
				keyword: this.keyword || '',
				...this.paging,
				order1: this.nowVal.order1 || 'ordering',
				order2: this.setpaixv(this.nowVal.val)
			}).then((res) => {
				const { code, data, msg } = res;
				if (code === 1) {
					if (data.length !== 0) this.list = this.list.concat(data);
					if (this.paging.pagenum === data.length) {
						this.paging.page += 1;
						this.status = 'loadmore';
					} else {
						this.status = 'nomore';
					}
				}
			});
		},

		setpaixv(val) {
			// 排序方法(desc倒序 asc正序)
			switch (val) {
				case 1:
					return 'asc';
					break;
				case 2:
					return 'desc';
					break;
				default:
					return 'asc';
					break;
			}
		},
		reset() {
			this.paging.page = 1;
			this.list = [];
		},
		handleItem(item) {
			this.reset();
			if (item.filter) {
				if (item.id === this.nowVal.id) item.val = this.nowVal.val === 1 ? 2 : 1 || 1;
			} else {
				this.tabs.forEach((item) => item.filter && (item.val = 1));
			}
			this.nowVal = item;
			this.getList();
		}
	}
};
</script>

<style lang="less" scoped>
.sesult {
	.container {
		padding: 24rpx 32rpx;
	}

	.searchbox {
		width: 750rpx;
		height: 88rpx;
		padding: 8rpx 32rpx;
		background: #ffffff;
		.search {
			display: flex;
			align-items: center;
			width: 686rpx;
			height: 72rpx;
			background: #f5f7f9;
			border-radius: 36rpx 36rpx 36rpx 36rpx;
			image {
				width: 28rpx;
				height: 28rpx;
				padding: 24rpx;
				.j-active();
			}
			input {
				flex: 1;
				font-size: 24rpx;
			}
			.splc {
				font-size: 24rpx;
				font-family: PingFang SC-Light, PingFang SC;
				font-weight: 300;
				color: #698a95;
			}
		}
	}
}
</style>
